<template>
  <div class="payPwd">
      <!-- <h2>支付密码修改</h2> -->
      <div class="title">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/layout/layoutshouye' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>账户安全</el-breadcrumb-item>
          <el-breadcrumb-item>支付密码修改</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="left fl">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="支付密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认支付密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
    name:"UserPaypsd",
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: ''
        },
        userId:'',
        timer:null,
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      };
    },
    mounted() {
      this.getUserId()
    },
    beforeDestroy() {
      clearTimeout(this.timer)
    },
    methods: {
      getUserId(){
        this.timer = setTimeout(()=>{
          this.userId = this.$store.state.user.userlist.userId
          // console.log(this.userId)
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.get("http://localhost:8080/api/upeateUserPay/updateUserPaylist",{
              params:{
                payPassword:this.ruleForm.pass,
                userId:this.userId
              }
            }).then(
              res=>{
                this.$message({
                  message:'用户信息修改成功',
                  type:'success'
                })
              },
              error=>{
                console.log("b",error.message)
              }
            )
            console.log("密码与确定密码为：",this.ruleForm.pass,this.ruleForm.checkPass,this.userId)
            // alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>

<style lang="less" scoped>
.payPwd {
  margin: 20px;
  .title{
    padding: 10px;
    background: #fff;
    border: 1px solid #eee;
    margin-bottom: 20px;
  }

  .left{
      width: 50%;
      height: 400px;
  }
}
</style>